package de.hfu.android;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.RatingBar;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;
import de.hfu.helper.DataAccess;
import de.hfu.helper.DataCarrier;
import de.hfu.helper.DataParser;
import de.hfu.model.DayPlan;
import de.hfu.model.Dish;

public class WeekViewListManager implements OnChildClickListener,
		OnClickListener {
	List<DayPlan> menuPlans;
	ItemFactory itemFactory;
	WeekViewListAdapter adapter;
	ExpandableListView listView;
	View headerView;
	Activity context;
	boolean flagLoadAll;
	private RetrieveDataTask retriever;

	public WeekViewListManager(Activity context) {
		this.context = context;
		itemFactory = new SimpleItemFactory(context);

		adapter = new WeekViewListAdapter(itemFactory);
		listView = (ExpandableListView) context
				.findViewById(R.id.expandableListView1);
		headerView = itemFactory.createMoreLessView(true, null);

		headerView.setOnClickListener(this);
		listView.addHeaderView(headerView);
		listView.setAdapter(adapter);

		listView.setOnChildClickListener(this);
		flagLoadAll = false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {

		Dish dish = (Dish) adapter.getChild(groupPosition, childPosition);
		if (!dish.getDescription(DataAccess.DEFAULT_LANGUAGE).equalsIgnoreCase(
				"kein Angebot")
				&& !dish.getTitle(DataAccess.DEFAULT_LANGUAGE)
						.equalsIgnoreCase("Kennzeichnung")) {
			DataCarrier.getInstance().setDish(dish);
			DataCarrier.getInstance().setDishKennzeichnung(adapter.getKennzeichnung(groupPosition));
			
			Intent intent = new Intent(context, MenuViewActivity.class);
			context.startActivity(intent);
		}
		return true;
	}

	public void reloadData() {
		retriever = new RetrieveDataTask(context, adapter, flagLoadAll);
		retriever.execute();
	}

	public RetrieveDataTask getTask() {
		return retriever;
	}

	@Override
	public void onClick(View v) {
		flagLoadAll = !flagLoadAll;
		TextView tv = (TextView) v;
		if (flagLoadAll)
			tv.setText(context.getString(R.string.viewless));
		else
			tv.setText(context.getString(R.string.viewmore));
		reloadData();
	}

}
